﻿using com.yoozoo.gta.Gameplay.PVE.Editor;
using NpcFramework;
using UnityEditor;
using UnityEngine;

namespace com.yoozoo.gta.SceneSeparate.Test.Editor
{
    [CanEditMultipleObjects]
    [CustomEditor(typeof (ScceneDataBind))]
    public class ScceneDataBindEditor : UnityEditor.Editor
    {
        private ScceneDataBind m_Target;
        private UnityEditor.Editor cacheEditor;
        private NpcBrithPointDataSO m_npcBrithPointDataSO;
        private Vector3 NpcEventPoint;
        private Vector3 NpcEventRotation;
        void OnEnable()
        {
            m_Target = target as ScceneDataBind;
            NpcEventPoint = m_Target.transform.position;
            NpcEventRotation = m_Target.transform.rotation.eulerAngles;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (m_Target.ObjType == SceneObjType.BirthPoint)
            {
                if (m_Target.BrithPointData == null)
                {
                    m_Target.BrithPointData = new NpcBrithPointData(); 
                }
                if (m_npcBrithPointDataSO == null)
                {
                    m_npcBrithPointDataSO = ScriptableObject.CreateInstance<NpcBrithPointDataSO>();
                    m_npcBrithPointDataSO.npcBrithPointData = m_Target.BrithPointData;
                    m_npcBrithPointDataSO.LoadBTreeByData();
                    m_Target.BrithPointData.Position = m_Target.transform.position;
                    m_Target.BrithPointData.Forward = m_Target.transform.forward;
                    m_Target.BrithPointData.RootName = m_Target.transform.name;
                }
                if (cacheEditor == null)
                {
                    cacheEditor = UnityEditor.Editor.CreateEditor(m_npcBrithPointDataSO);
                }
                cacheEditor.OnInspectorGUI();
            }
            else if (m_Target.ObjType == SceneObjType.NpcGroup)
            {
                m_Target.GroupId = EditorGUILayout.IntField("groupId", m_Target.GroupId);
                m_Target.BrithPointData.GroupId = m_Target.GroupId;
                m_Target.BrithPointData.Position = m_Target.transform.position;
            }
            else if (m_Target.ObjType == SceneObjType.Event)
            {
                m_Target.TriggerSize = EditorGUILayout.Vector3Field("触发器大小", m_Target.TriggerSize);
                m_Target.EventName = EditorGUILayout.TextField("事件名称", m_Target.EventName);
                m_Target.IsNpcEvent = EditorGUILayout.Toggle("是否是NPC触发事件", m_Target.IsNpcEvent);
                if (m_Target.IsNpcEvent)
                {
                    if (m_Target.BrithPointData == null)
                    {
                        m_Target.BrithPointData = new NpcBrithPointData();
                    }
                    m_Target.BrithPointData.Forward = m_Target.transform.forward;
                    m_Target.BrithPointData.TriggerPosition = EditorGUILayout.Vector3Field("主角所在位置", m_Target.BrithPointData.TriggerPosition);
                    var rotation = Quaternion.FromToRotation(Vector3.forward, m_Target.BrithPointData.TriggerForward);
                    var targetRotate= EditorGUILayout.Vector3Field("主角所在朝向", rotation.eulerAngles);
                    m_Target.BrithPointData.TriggerForward = Quaternion.Euler(targetRotate) * Vector3.forward;
                    m_Target.BrithPointData.EventId =
                        EditorGUILayout.IntField("事件Id", m_Target.BrithPointData.EventId);
                }
            }
            else
            {
                m_Target.BrithPointData.Position = m_Target.transform.position;
            }

            m_Target.NpcBornProbability = EditorGUILayout.FloatField("NPC出生概率", m_Target.NpcBornProbability);
        }
    }
}
